package com.liu.springcloud.controller;

import com.liu.springcloud.entities.CommonResult;
import com.liu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {
//    final static String URL = "http://localhost:8001";
    final static String URL = "http://PAYMENT-SERVICE";
    @Autowired
    private RestTemplate template;


    @PostMapping("/consumer/create")
    public CommonResult<Payment> create(Payment payment){
        return template.postForObject(URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return template.getForObject(URL+"/payment/get/"+id,CommonResult.class);
    }

    @GetMapping("/consumer/getEnt/{id}")
    public CommonResult<Payment> getEnt(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> r = template.getForEntity(URL+"/payment/get/"+id,CommonResult.class);
        if (r.getStatusCode().is2xxSuccessful()){
            return r.getBody();
        }else {
            return new CommonResult<>(444,"操作失败");
        }
    }

}
